//
//  YSYTabBarController.m
//  YSYUISateRestorationDemo
//
//  Created by muxue on 2019/9/27.
//  Copyright © 2019 暮雪. All rights reserved.
//

#import "YSYTabBarController.h"
#import "ViewController.h"
#import "PersonDetailController.h"
#import "YSYNavigationController.h"

@interface YSYTabBarController ()
@end

@implementation YSYTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.whiteColor;
    self.restorationClass = self.class;
    self.restorationIdentifier = NSStringFromClass(self.class);
    [self settingChildrenViewController];
}

// 还原 App 上一次浏览的页面
- (void)setLastViewController:(UIViewController *)lastViewController {
    if (!lastViewController) {
           return;
    }
   for (NSInteger i = 0; i < self.viewControllers.count; i++) {
       YSYNavigationController *viewController = self.viewControllers[i];
       YSYBaseViewController *vc = viewController.viewControllers.lastObject;
       if ([vc isKindOfClass:lastViewController.class]) {
           self.selectedIndex = i;
           self.lastViewController = nil;
           return;
       }
   }
    YSYNavigationController *homeNavigationController = self.viewControllers.firstObject;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
       [homeNavigationController pushViewController:lastViewController animated:NO];
    });
}

// 设置子视图
- (void)settingChildrenViewController {
    ViewController *tableVC = [[ViewController alloc] init];
    YSYNavigationController *tabOneNav = [[YSYNavigationController alloc] initWithRootViewController:tableVC];
    tabOneNav.tabBarItem.title = @"列表";
    
    PersonDetailController *settingVc = [[PersonDetailController alloc] init];
    YSYNavigationController *tabTwoNav = [[YSYNavigationController alloc] initWithRootViewController:settingVc];
    tabTwoNav.tabBarItem.title = @"设置";
    self.viewControllers = @[tabOneNav, tabTwoNav];
}

- (void)decodeRestorableStateWithCoder:(NSCoder *)coder {
    [super decodeRestorableStateWithCoder:coder];
}

- (void)encodeRestorableStateWithCoder:(NSCoder *)coder {
    [super encodeRestorableStateWithCoder:coder];
}

@end
